#include <nice2d/widgets/controlradioselector.h>

class ControlSelectorData
{
public:
    std::list<std::weak_ptr<Control>> controls;
    std::weak_ptr<Control> selected;
};

ControlRadioSelector::ControlRadioSelector():
    data(new ControlSelectorData())
{
}

ControlRadioSelector::~ControlRadioSelector()
{
}

void ControlRadioSelector::addControl(ControlPointer control)
{
    if (control)
        data->controls.push_back(control);
}

void ControlRadioSelector::select(ControlPointer control)
{
    if (control) {
        selected(control);
        data->selected = control;

        auto itr = data->controls.begin();
        while (itr != data->controls.end()) {
            if(!itr->expired())
            {
                auto current = itr->lock();
                if (current != control)
                    unselected(current);
            }
            itr ++;
        }
    }
}

std::weak_ptr<Control> ControlRadioSelector::getSelectedControl() const
{
    return data->selected;
}

void ControlRadioSelector::unselected(ControlPointer control)
{
    (void)control;
}

void ControlRadioSelector::selected(ControlPointer control)
{
    (void)control;
}
